import json

import requests

k = open('city.json',encoding = 'utf-8').read()
city = json.loads(k)
go_city = input("请输入出发城市：")
to_city = input("请输入到达城市：")
sj = input("请输入出发时间：")

url = f"https://kyfw.12306.cn/otn/leftTicket/queryG?leftTicketDTO.train_date={sj}&leftTicketDTO.from_station={city[go_city]}&leftTicketDTO.to_station={city[to_city]}&purpose_codes=ADULT"


headers = {
    "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0",
    "cookie ": "_uab_collina=174646404451669250709201; JSESSIONID=382B436D531EACDF3C592D7AB3152B9F; BIGipServerotn=1473839370.50210.0000; BIGipServerpassport=937951498.50215.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; route=9036359bb8a8a461c164a04f8f50b252; _jc_save_fromStation=%u5E7F%u5DDE%2CGZQ; _jc_save_toStation=%u4E0A%u6D77%2CSHH; _jc_save_fromDate=2025-05-06; _jc_save_toDate=2025-05-06; _jc_save_wfdc_flag=dc",
    "referer" : "https://kyfw.12306.cn/otn/leftTicket/init?"
}

res = requests.get(url, headers=headers)

JSON = res.json()
data = JSON["data"]["result"]
page_num = 0
for i in data:
    index = i.split("|")
    page_num += 1
    che_ci = index[3] #车次
    go_time = index[8] #始发时间
    top_time = index[9] #到达时间
    time = index[10] #历时
    nb_vip = index[32] #商务
    vip = index[31] #一等
    pt = index[30] #二等

    dit = {
        '序号':page_num,
        '车次':che_ci,
        '出发时间':go_time,
        '到达时间':top_time,
        '历时':time,
        '商务座':nb_vip,
        '一等座':vip,
        '二等座':pt


    }
    print(dit)
page_num = input("请输入对应的车序号：")
